package dal;

import inftrastructure.exceptions.ClientRuntimeException;

import java.io.IOException;
import java.io.Serializable;

import core.ServerSession;

import remote.RemoteInvocation;
import server.IServerSession;
import server.querry.Condition;


/**
 * The persistent class for the pets database table.
 * 
 */
public class Pet implements IPet,Serializable {
	private static final long serialVersionUID = 1L;
	
	private int id;
	private String long_desc;
	private Pet_type pet_type;
	private User deliver_User; 
	private byte[] picture;
	private String short_desc;	
	private String name;
	
	/** Keeps attached pet session */
	private transient ServerSession attachedPetSession;
	
  
    public String getName() {
		return name;
	}


    public Pet(Pet_type type,String name,String shortDesc,String longDesc,byte[] picture){
    	this.name = (null != name)?name:"";;
    	this.pet_type = type;
    	this.short_desc = (null != shortDesc)?shortDesc:"";
    	this.long_desc = (null != longDesc)?longDesc:"";    	
    	this.picture = picture;
    }
    
    
    public void setSession(ServerSession session){
    	this.attachedPetSession = session;
    }
    
    public ServerSession getSession(){
    	return this.attachedPetSession;
    }
    
    public Pet(Pet_type type,String name,String shortDesc,String longDesc){
    	this(type, name, shortDesc, longDesc, null);
    }
    
    public Pet(Pet_type type,String name,String shortDesc){
    	this(type, name, shortDesc, null, null);
    }
    
    
	public int getId() {
		return this.id;
	}
    
	public String getLong_desc() {
		return this.long_desc;
	}


	public Pet_type getPet_type() {
		return pet_type;
	}
 


	public User getDeliver_user() {
		return deliver_User;
	}



	public byte[] getPicture() {
		
		// check if picture is not loaded yet
		if (null == picture){			
			picture = getSession().getPetPicture(this.getId());
		}
		return this.picture;
	}


	public String getShort_desc() {
		return this.short_desc;
	}

	
	/**
	 * This method is invoked when passing the pet to the server
	 * @param out
	 * @throws IOException
	 */
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeInt(id);
		out.writeUTF(long_desc);
		out.writeUTF(name);
		out.writeUTF(short_desc);
		out.writeObject(pet_type);
		out.writeObject(deliver_User);
		
		if (null != picture){
			out.writeInt(picture.length);
			out.write(picture);
		}else{
			out.writeInt(0);
		}

	}
	
	/**
	 * This method is invoked when reading the pet from the server
	 * @param in
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
		
		id = in.readInt();
		long_desc = in.readUTF();
		name = in.readUTF();
		short_desc = in.readUTF();
		pet_type = (Pet_type)in.readObject();
		deliver_User = (User)in.readObject();
		int length = in.readInt();
		
		if (0 != length){
			picture = new byte[length];
			in.readFully(picture);	
		}else{
			picture = null;
		}
	}

}